package com.wsl.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @Author:WangShuliang
 * @Description:
 * @Date:Created in 19:05 2017/11/24
 * @Modified by:
 */
@Configuration
@ComponentScan("com.wsl")
@EnableAsync//利用@EnableAsync 注解开启异步任务支持
public class TaskExecutorConfig  implements AsyncConfigurer{
    /**
     * 配置类实现AsyncConfigurer接口重写getAsyncExecutor方法
     * 并返回一个ThreadPoolTaskExecutor这样我们就获得了一个基于线程池taskExecutor
     *
     * @return
     */
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(5);//线程池维护线程的最小数目
        taskExecutor.setMaxPoolSize(15);//线程池维护线程的最大数量
        taskExecutor.setQueueCapacity(5);//缓冲任务队列的长度
        taskExecutor.initialize();//线程池进行初始化
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }
}